package com.example.demo.controller;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shenb
 * @date 2022-07-08 16:46
 */
@Validated
@RestController
@RequestMapping("users")
public class UserController {

    @GetMapping
    public List<User> getUsers(
            @RequestParam(value = "name", required = false) String name,
            @Min(1) @Max(100)
            @RequestParam(name = "limit", required = false, defaultValue = "10") int limit
    ) {
        return new ArrayList<>();
    }

    @PostMapping
    public void update(@Valid @RequestBody User user) {
        return;
    }

    @PutMapping
    public User create(@Valid @RequestBody User user) {
        return new User();
    }


    @DeleteMapping("{id}")
    public void delete(@PathVariable("id") Long id) {

    }

    static class User {
        public Long id;

        @NotBlank(message = "Name is mandatory")
        public String name;

        @Max(200)
        @NotNull
        public Integer age;
    }
}
